//
//  NSLabel.swift
//  JKTools
//
//  Created by 姜奎 on 2024/10/14.
//

import Cocoa

class NSBorderLabel: NSView {

    weak var delegate: NSTextFieldDelegate?

    var cornerRadius: CGFloat = 8.0

    var dashPattern: [CGFloat] = [1.0, 1.0]

    // 边框宽度
    var borderWidth: CGFloat = 1.0

    // 文字与边框的间距
    var padding: CGFloat = 5.0

    var borderColor: NSColor = .red

    var stringValue: String = "" {
        didSet {
            self.contentLab.stringValue = stringValue
        }
    }

    private lazy var contentLab: NSTextField = {
        let contentLab = NSTextField()
        contentLab.delegate = self
        contentLab.alignment = .center
        contentLab.maximumNumberOfLines = 1
        contentLab.backgroundColor = .clear
        contentLab.textColor = .black
        contentLab.isEditable = false
        contentLab.isSelectable = true
        contentLab.isBordered = false
        contentLab.refusesFirstResponder = true
        contentLab.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(contentLab)
        let topAnchor = contentLab.topAnchor.constraint(greaterThanOrEqualTo: self.topAnchor)
        let leftAnchor = contentLab.leftAnchor.constraint(equalTo: self.leftAnchor, constant: padding)
        let centerXAnchor = contentLab.centerXAnchor.constraint(equalTo: self.centerXAnchor)
        let centerYAnchor = contentLab.centerYAnchor.constraint(equalTo: self.centerYAnchor)
        self.addConstraints([topAnchor,leftAnchor,centerXAnchor,centerYAnchor])
        return contentLab
    }()

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        // 保存当前图形上下文状态
        if stringValue.isEmpty {
            return
        }
        guard let context = NSGraphicsContext.current?.cgContext else { return }

        context.saveGState()

        // 绘制边框
        let path = NSBezierPath(roundedRect: bounds, xRadius: cornerRadius, yRadius: cornerRadius)

        path.addClip()

        context.setLineDash(phase: 0, lengths: dashPattern)

        context.setStrokeColor(borderColor.cgColor)

        context.setLineWidth(borderWidth)

        path.stroke()

        // 恢复图形上下文状态
        context.restoreGState()
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    private func setup() {
        translatesAutoresizingMaskIntoConstraints = false
    }

    override func setContentCompressionResistancePriority(_ priority: NSLayoutConstraint.Priority, for orientation: NSLayoutConstraint.Orientation) {
        super.setContentCompressionResistancePriority(priority, for: orientation)
        contentLab.setContentCompressionResistancePriority(priority, for: orientation)
    }
}

extension NSBorderLabel: NSTextFieldDelegate {
    @objc func controlTextDidBeginEditing(_ obj: Notification) {
        delegate?.controlTextDidBeginEditing?(obj)
    }

    @objc func controlTextDidEndEditing(_ obj: Notification) {
        self.stringValue = contentLab.stringValue
        delegate?.controlTextDidChange?(obj)
    }
}

class NSImageLabel: NSView {

    weak var delegate: NSTextFieldDelegate?

    var image: NSImage? = nil {
        didSet {
            if let image = image {
                self.imageView.image = image
                contentView.insertArrangedSubview(imageView, at: 0)
            } else {
                imageView.removeFromSuperview()
                contentView.removeArrangedSubview(imageView)
            }
        }
    }

    private lazy var imageView: NSImageView = {
        let imageView = NSImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()

    var stringValue: String = "" {
        didSet {
            self.contentLab.stringValue = stringValue
        }
    }

    var isSelectable: Bool = false {
        didSet {
            self.contentLab.isSelectable = isSelectable
        }
    }

    var isEditable: Bool = false {
        didSet {
            self.contentLab.isEditable = isEditable
        }
    }

    private lazy var contentLab: NSTextField = {
        let contentLab = NSTextField()
        contentLab.delegate = self
        contentLab.alignment = .center
        contentLab.maximumNumberOfLines = 1
        contentLab.backgroundColor = .clear
        contentLab.textColor = .black
        contentLab.isEditable = false
        contentLab.isSelectable = false
        contentLab.isBordered = false
        contentLab.refusesFirstResponder = true
        contentLab.translatesAutoresizingMaskIntoConstraints = false
        contentView.addArrangedSubview(contentLab)
        return contentLab
    }()

    private lazy var contentView: NSStackView = {
        let contentView = NSStackView()
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.orientation = .horizontal
        contentView.alignment = .centerY
        contentView.distribution = .equalSpacing
        contentView.spacing = 0
        self.addSubview(contentView)
        let topAnchor = contentView.topAnchor.constraint(equalTo: self.topAnchor)
        let leftAnchor = contentView.leftAnchor.constraint(equalTo: self.leftAnchor)
        let centerXAnchor = contentView.centerXAnchor.constraint(equalTo: self.centerXAnchor)
        let centerYAnchor = contentView.centerYAnchor.constraint(equalTo: self.centerYAnchor)
        self.addConstraints([topAnchor,leftAnchor,centerXAnchor,centerYAnchor])
        return contentView
    }()

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    private func setup() {
        translatesAutoresizingMaskIntoConstraints = false
    }

    override func setContentCompressionResistancePriority(_ priority: NSLayoutConstraint.Priority, for orientation: NSLayoutConstraint.Orientation) {
        super.setContentCompressionResistancePriority(priority, for: orientation)
        contentLab.setContentCompressionResistancePriority(priority, for: orientation)
    }
}

extension NSImageLabel: NSTextFieldDelegate {
    @objc func controlTextDidBeginEditing(_ obj: Notification) {
        delegate?.controlTextDidBeginEditing?(obj)
    }

    @objc func controlTextDidEndEditing(_ obj: Notification) {
        self.stringValue = contentLab.stringValue
        delegate?.controlTextDidChange?(obj)
    }
}
